module.exports = class extends think.Mongo {

	/**
	 * @author: ares
	 * @date: 2019/4/3 10:03
	 * @description: 新建
	 * @param {object}data
	 * @returns {Promise.<*>}
	 */
	async $create(data) {
		data.password = 'e10adc3949ba59abbe56e057f20f883e';
		data.birthday = think.datetime(data.birthday, 'YYYY-MM-DD');
		data.createTime = think.datetime(+new Date);
		let find = await this.where({account: data.account}).find();
		if (think.isEmpty(find)) return await this.add(data);
		return null;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:50
	 * @description: 删除
	 * @param {string}_id
	 * @returns {Promise.<*>}
	 */
	async $delete(_id) {
		let find = await await this.where({_id}).find();
		if (think.isEmpty(find)) return null;
		return await this.where({_id}).delete();
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:29
	 * @description: 更新
	 * @param {object}data
	 * @returns {Promise.<*>}
	 */
	async $update(data) {
		data.birthday = think.datetime(data.birthday, 'YYYY-MM-DD');
		data.updateTime = think.datetime(+new Date);
		let find = await this.where({_id: data._id}).find();
		if (think.isEmpty(find)) return null;
		return await this.where({_id: data._id}).update(data);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:37
	 * @description: 切换用户启用禁用状态
	 * @param data
	 * @returns {Promise.<*>}
	 */
	async toggleState(data) {
		data.updateTime = think.datetime(+new Date);
		return await this.where({_id: data._id}).updateInfo(data);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:39
	 * @description: 重置用户密码
	 * @param {object}data 用户信息
	 * @returns {Promise.<*>}
	 */
	async resetPwd(data) {
		data.updateTime = think.datetime(+new Date);
		data.password = 'e10adc3949ba59abbe56e057f20f883e';
		return await this.where({_id: data._id}).updateInfo(data);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/10 16:44
	 * @description: 用户修改自己的密码
	 * @param {string}account    用户账户
	 * @param {string}oldPwd
	 * @param {string}newPwd
	 * @returns {Promise.<*>}
	 */
	async modifyPwd(account, oldPwd, newPwd) {
		let find = await await this.where({account}).find(),
			data = {
				account: account,
				password: newPwd
			};
		if (think.isEmpty(find)) return null;
		return await this.where({account: account, password: oldPwd}).update(data);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 13:40
	 * @description: 分页查询,可搜索
	 * @param {string}searchKey   关键字
	 * @param {number}currentPage
	 * @param {number}pageSize
	 * @returns {Promise.<*>}
	 */
	async getListByPage(searchKey, currentPage, pageSize) {
		let result = await this.where({'name|account': ['like', '%' + searchKey + '%']})
			.order(['createTime DESC'])
			.page(currentPage, pageSize)
			.countSelect();
		return result;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:43
	 * @description: 根据id获取信息
	 * @param {string}_id
	 * @returns {Promise.<*>}
	 */
	async getById(_id) {
		return await this.where({_id}).find();
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:47
	 * @description: 根据账号获取用户信息
	 * @param {string}account
	 * @returns {Promise.<*>}
	 */
	async getByAccount(account) {
		return await this.where({account}).find();
	}

	/**
	 * @author: ares
	 * @date: 2020/3/8 15:21
	 * @description: 获取登录用户信息
	 * @param {string}account
	 * @param {string}password
	 * @returns {Promise.<*>}
	 */
	async getLoginInfo(account, password) {
		return await this.where({account, password}).find();
	}


	/**
	 * @author: ares
	 * @date: 2019/4/10 11:04
	 * @description: 记录登录/登出信息
	 * @param {object}data
	 * @param {boolean}logout   退出登录
	 * @returns {Promise.<*>}
	 */
	async recordLogin(data, logout) {
		let loginInfoModel = this.model('loginInfo'),
			result = logout ? await loginInfoModel.thenUpdate(data, {token: data.token})
				: await loginInfoModel.thenUpdate(data, {_id: data._id});
		return result;
	}
}